using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SS.core
{
	internal class provider
	{
		private Func objFunc = new Func();
		public provider()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public void loadToDataGridView(DataGridView dgv, BindingNavigator bdn)
		{
			dgv.AutoGenerateColumns = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string selectCommandText = "SELECT CusID, Name, Address, Phone FROM [TabPartner] WHERE [IsProvider] = 1 ORDER BY [Name]";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
			DataSet dataSet = new DataSet();
			sqlDataAdapter.Fill(dataSet, "TabPartner");
			BindingSource bindingSource = new BindingSource();
			bindingSource.DataSource = dataSet.Tables["TabPartner"];
			bdn.BindingSource = bindingSource;
			dgv.DataSource = bindingSource;
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void loadToListView(ListView lv, string searchFor)
		{
			lv.Items.Clear();
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT CusID,Name,Address FROM [TabPartner] WHERE [Name] LIKE N'%" + searchFor + "%' AND [IsProvider] = 1 ORDER BY [Name]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				lv.Items.Add((num + 1).ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add(sqlDataReader["ProID"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["Name"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["Address"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void loadToListViewByPhone(ListView lv, string PhoneSearch)
		{
			lv.Items.Clear();
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT CusID,Name,Address FROM [TabPartner] WHERE UPPER([Phone]) LIKE N'%" + PhoneSearch.ToUpper() + "%' AND [IsProvider] = 1 ORDER BY [Name]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				lv.Items.Add((num + 1).ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add(sqlDataReader["ProID"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["Name"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["Address"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getInfo(string pID, TextBox txtID, ComboBox cbbProGroup, TextBox txtName, TextBox txtAddress, TextBox txtPhone, TextBox txtFax, TextBox txtEmail, TextBox txtTax, TextBox txtGCustomer_Notes, TextBox txtWebsite, TextBox txtContactor, CheckBox ckb, TextBox txtCusID)
		{
			txtName.Text = "";
			txtName.Text = "";
			txtAddress.Text = "";
			txtPhone.Text = "";
			txtFax.Text = "";
			txtEmail.Text = "";
			txtTax.Text = "";
			txtGCustomer_Notes.Text = "";
			txtWebsite.Text = "";
			txtContactor.Text = "";
			ckb.Checked = false;
			txtCusID.Text = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabPartner] WHERE [CusID] = '" + pID.ToString() + "' AND [IsProvider] = 1 ";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				txtID.Text = sqlDataReader["CusID"].ToString();
				txtName.Text = sqlDataReader["Name"].ToString();
				txtAddress.Text = sqlDataReader["Address"].ToString();
				txtPhone.Text = sqlDataReader["Phone"].ToString();
				txtFax.Text = sqlDataReader["Fax"].ToString();
				txtEmail.Text = sqlDataReader["Email"].ToString();
				txtTax.Text = sqlDataReader["Tax"].ToString();
				txtWebsite.Text = sqlDataReader["Website"].ToString();
				txtContactor.Text = sqlDataReader["Contactor"].ToString();
				if (sqlDataReader["IsCustomer"].ToString() == "1")
				{
					ckb.Checked = true;
				}
				else
				{
					ckb.Checked = false;
				}
				cbbProGroup.SelectedValue = sqlDataReader["CusGroup"].ToString();
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getInfo(string pID, TextBox txtID, TextBox txtName, TextBox txtAddress, TextBox txtPhone, TextBox txtFax, TextBox txtEmail, TextBox txtTax, TextBox txtGCustomer_Notes, TextBox txtWebsite, TextBox txtContactor, CheckBox ckb, TextBox txtCusID)
		{
			txtName.Text = "";
			txtName.Text = "";
			txtAddress.Text = "";
			txtPhone.Text = "";
			txtFax.Text = "";
			txtEmail.Text = "";
			txtTax.Text = "";
			txtGCustomer_Notes.Text = "";
			txtWebsite.Text = "";
			txtContactor.Text = "";
			ckb.Checked = false;
			txtCusID.Text = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabPartner] WHERE [CusID] = '" + pID.ToString() + "' AND [IsProvider] = 1";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				txtID.Text = sqlDataReader["ProID"].ToString();
				txtName.Text = sqlDataReader["Name"].ToString();
				txtAddress.Text = sqlDataReader["Address"].ToString();
				txtPhone.Text = sqlDataReader["Phone"].ToString();
				txtFax.Text = sqlDataReader["Fax"].ToString();
				txtEmail.Text = sqlDataReader["Email"].ToString();
				txtTax.Text = sqlDataReader["Tax"].ToString();
				txtGCustomer_Notes.Text = sqlDataReader["CusNote"].ToString();
				txtWebsite.Text = sqlDataReader["Website"].ToString();
				txtContactor.Text = sqlDataReader["Contactor"].ToString();
				if (sqlDataReader["CusID"].ToString() != "")
				{
					ckb.Checked = true;
					txtCusID.Text = sqlDataReader["CusID"].ToString();
				}
				else
				{
					ckb.Checked = false;
					txtCusID.Text = "";
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getInfo(string pID, TextBox txtName, Label txtAddress, Label txtPhone)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT Name,Address,Phone FROM [TabPartner] WHERE [ProID] = '" + pID.ToString() + "' AND [ProID] <> ''";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				while (sqlDataReader.Read())
				{
					txtName.Text = sqlDataReader["Name"].ToString();
					txtAddress.Text = sqlDataReader["Address"].ToString();
					txtPhone.Text = sqlDataReader["Phone"].ToString();
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int del(string id)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("DelPartner", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("@ID", SqlDbType.VarChar);
				sqlCommand.Parameters.Add("IsProvider", SqlDbType.SmallInt);
				sqlCommand.Parameters["@ID"].Value = id;
				sqlCommand.Parameters["IsProvider"].Value = 1;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
	}
}
